<script setup>
import useAdminrouter from "@/router/index.js";
import {reactive, ref, onMounted} from 'vue'
import AvatarIcon from '@/assets/images/avatar.png'
import {shopCustomStore} from '@/stores/modules/shop/index.js'
import {
  GetCustomInfoMessageService,
  addCustomInfoService
} from '@/api/netshop/customInfoMessage.js'
import {ElMessage} from "element-plus";

const router = useAdminrouter
onMounted(() => {
  getList()
})
const loading = ref(true)
const store = shopCustomStore()
const isRegister = ref(true)
let tData = reactive({
  form: {
    avatar: '',
    name: undefined,
    gender: undefined,
    mobile: undefined,
    description: undefined,
  }
})
let getData = ref({
  avatar: '',
  name: undefined,
  gender: undefined,
  mobile: undefined,
  created_time: undefined,
  description: undefined,
})

const getList = async () => {
  const id = store.custom_id
  await GetCustomInfoMessageService(id)
      .then((res) => {
        getData.value = res.data.data
        loading.value = false
      })
      .catch((err) => {
        console.log(err)
      })
}

const save = async () => {
  let userId = store.custom_id
  loading.value = true
  console.log(tData.form)
  await addCustomInfoService(tData.form, userId)
      .then((res) => {
        console.log(res)
        localStorage.setItem('custom_user_name', res.data.data.name)
        store.custom_user_name = res.data.data.name
        setTimeout(() => {
          isRegister.value = true
          loading.value = false
        }, 2000)
      })
      .catch((err) => {
        console.log(err)
      })
}

// 图片上传的token
const uploadHeaders = ref({
  Authorization: 'bearer' + ' ' + localStorage.getItem('admin_token')
})

// 上传前的图片校验
const beforeAvatarUpload = (rawFile) => {
  if (rawFile.type !== "image/jpg" && rawFile.type !== "image/png" && rawFile.type !== "image/jpeg") {
    console.log(rawFile)
    ElMessage.error('文件格式必须是jpg、jpeg或者png')
    return false
  } else if (rawFile.size / 1024 / 1024 > 20) {
    ElMessage.error('该文件格式不能超过 20MB!')
    return false
  }
  return true
}
// 图片先上传回服务器然后再将图片本地的地址返回来，再走drf将图片存放本地的路径存到数据库
const handleAvatarSuccess = async (response, file) => {
  tData.form.avatar = response['0']
  console.log('图片的地址', tData.form.avatar)
}
</script>

<template>
  <div>
    <!--    展示个人信息     -->
    <el-card class="box-card" v-if="isRegister" v-loading="loading">
      <template #header>
        <div class="card-header">
          <span>个人信息</span>
          <el-link type="info" :underline="false" @click="isRegister = false">编辑</el-link>
        </div>
      </template>
      <div class="list-content">
        <div class="edit-view">
          <div class="item flex-view">
            <div class="label">头像</div>
            <div class="right-box avatar-box flex-view">
              <el-image v-if="getData && getData.avatar" :preview-src-list="[getData.avatar]" :src="getData.avatar" class="avatar"></el-image>
              <el-image v-else :src="AvatarIcon" class="avatar"></el-image>
            </div>

          </div>
          <div class="split-line"></div>
          <div class="item flex-view">
            <div class="label">昵称</div>
            <div class="right-box">
              <h3>{{ getData.name||'尊敬的新用户' }}</h3>
            </div>
          </div>
          <div class="split-line"></div>
          <div class="item flex-view">
            <div class="label">手机号</div>
            <div class="right-box">
              <h3>{{ getData.mobile }}</h3>
            </div>
          </div>
          <div class="split-line"></div>
          <div class="item flex-view">
            <div class="label">性别</div>
            <div class="right-box">
              <h3>{{ getData.gender === '1' ? '男' : '女' }}</h3>
            </div>
          </div>
          <div class="split-line"></div>
          <div class="item flex-view">
            <div class="label">创建时间</div>
            <div class="right-box">
              <h3>{{ getData.created_time }}</h3>
            </div>
          </div>
          <div class="split-line"></div>
          <div class="item flex-view">
            <div class="label">个人简介</div>
            <div class="right-box">
              <h3>{{ getData.description || '无' }}</h3>
            </div>
          </div>
        </div>
      </div>
    </el-card>
    <!--    编辑个人信息      -->
    <el-card class="box-card" v-loading="loading" v-else>
      <template #header>
        <div class="card-header">
          <span>编辑个人信息</span>
          <el-link type="info" :underline="false" @click="isRegister = true">返回</el-link>
        </div>
      </template>
      <div class="edit-content">
        <div class="edit-view">
          <div class="item flex-view">
            <div class="label">头像</div>
            <div class="right-box avatar-box flex-view">
              <el-image v-if="getData.avatar" :src="getData.avatar" class="avatar"></el-image>
              <el-image v-else :src="AvatarIcon" class="avatar"></el-image>
              <div class="change-tips flex-view">
                <el-upload
                    action="http://127.0.0.1:8000/netshop/uploadmessage/"
                    :headers="uploadHeaders"
                    :multiple="false"
                    :before-upload="beforeAvatarUpload"
                    :on-preview="handlePreview"
                    :on-remove="handleRemove"
                    :limit="1"
                    :on-success="handleAvatarSuccess"
                >
                  <label>点击更换头像</label>
                </el-upload>
                <p class="tip">图片格式支持 PNG、JPEG，尺寸不小于 200 PX，小于 20 MB</p>
              </div>
            </div>
          </div>
          <div class="item flex-view">
            <div class="label">昵称</div>
            <div class="right-box">
              <el-input type="text" v-model="tData.form.name" placeholder="请输入昵称" maxlength="20"
                        class="input-dom"></el-input>
              <p class="tip">支持中英文，长度不能超过 20 个字符</p>
            </div>
          </div>
          <div class="item flex-view">
            <div class="label">手机号</div>
            <div class="right-box">
              <el-input type="text" v-model="tData.form.mobile" placeholder="请输入邮箱" maxlength="100"
                        class="input-dom web-input"></el-input>
            </div>
          </div>
          <div class="item flex-view">
            <div class="label">性别</div>
            <div class="right-box">
              <el-select class="input-dom web-input" v-model="tData.form.gender" placeholder="请选择你的性别">
                <el-option label="男" value="1"/>
                <el-option label="女" value="2"/>
              </el-select>
            </div>
          </div>
          <div class="item flex-view">
            <div class="label">个人简介</div>
            <div class="right-box">
              <el-input type="textarea" rows="10" v-model="tData.form.description" maxlength="200"
                        clearable="true"></el-input>
              <p class="tip">限制200字以内</p>
            </div>
          </div>
        </div>
      </div>
      <template #footer>
        <div class="footer">
          <el-button size="large" round type="primary" @click="save">保存</el-button>
        </div>
      </template>
    </el-card>
  </div>
</template>

<style scoped>
.box-card {
  width: 100%;
  height: 100%;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.card-header span {
  color: #152844;
  font-weight: 600;
  font-size: 18px;
  line-height: 48px;
  height: 48px;
  margin-bottom: 4px;
  border-bottom: 1px solid #cedce4;
}

/*横线*/
.split-line {
  width: 100%;
  height: 2px;
  background: #dae6f9;
}

/*展示页*/
.list-content {
  text-align: center;
}

.list-content .el-image {
  margin: 0 auto;
}

.flex-view {
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
}

.item {
  -webkit-box-align: center;
  -ms-flex-align: center;
  align-items: center;
  margin: 20px 0;
}

.label {
  width: 100px;
  color: #152844;
  font-weight: 600;
  font-size: 14px;
}

.right-box {
  -webkit-box-flex: 1;
  -ms-flex: 1;
  flex: 1;
}

.avatar {
  width: 64px;
  height: 64px;
  border-radius: 50%;
  margin-right: 16px;
}

.change-tips {
  -webkit-box-align: center;
  -ms-flex-align: center;
  align-items: center;
  -ms-flex-wrap: wrap;
  flex-wrap: wrap;
}

label {
  color: #4684e2;
  font-size: 14px;
  line-height: 22px;
  height: 22px;
  cursor: pointer;
  width: 90px;
  display: block;
}

.tip {
  color: #6f6f6f;
  font-size: 14px;
  height: 22px;
  line-height: 22px;
  margin: 0;
  width: 100%;
}

.right-box {
  -webkit-box-flex: 1;
  -ms-flex: 1;
  flex: 1;
}

.input-dom {
  width: 400px;
}

.input-dom {
  background: #f8fafb;
  border-radius: 4px;
  height: 40px;
  line-height: 40px;
  font-size: 14px;
  color: #152844;
  padding: 0 12px;
}

.tip {
  font-size: 12px;
  line-height: 16px;
  color: #6f6f6f;
  height: 16px;
  margin-top: 4px;
}

.intro {
  resize: none;
  background: #f8fafb;
  width: 100%;
  padding: 8px 12px;
  height: 82px;
  line-height: 22px;
  font-size: 14px;
  color: #152844;
}

.footer {
  float: right;
}
</style>